package com.foreveross.proxyip.util;

import javax.servlet.ServletContext;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class SpringBeanUtil {
	private static ApplicationContext applicationContext = null;
	/**
	 * 获取一个Bean
	 * @author liyongqiang
	 * @param beanName
	 * @return
	 */
//	public static Object getBean(String beanName) {
//		if (applicationContext != null) {
//			return applicationContext.getBean(beanName);
//		} else {
//			return null;
//		}
//	}
	

	/**
	 * 获取一个Bean
	 * 
	 * @author liyongqiang
	 * @param servletContext
	 * @param beanName
	 * @return
	 */
	public static Object getBean(ServletContext servletContext, String beanName) {
		WebApplicationContext spring = WebApplicationContextUtils
				.getWebApplicationContext(servletContext);
		if (spring != null) {
			return spring.getBean(beanName);
		} else {
			return null;
		}
	}

	public static ApplicationContext getApplicationContext() {
		return applicationContext;
	}

	public static void setApplicationContext(
			ApplicationContext _applicationContext) throws BeansException {
		applicationContext = _applicationContext;
	}

}
